<?php

namespace App\Admin\Controllers;

use App\Models\Classify;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class ClassifyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '分类列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Classify);
        $grid->model()->orderBy('sort','asc');

        $grid->column('id', __('Id'));
        $grid->column('icon', __('Icon'))->image(50, 50);
        $grid->column('name', __('名称'))->expand(function ($model){
            $books = $model->books()->get()->map(function ($comment){
                return $comment->only(['name','cover']);
            });

            return new Table(['书本名称','封面' ], $books->toArray());
        });
        $grid->column('description', __('描述'));
        $grid->column('sort', __('排序'))->sortable();
       // $grid->column('is_show', __('是否显示'));
        $grid->is_show('是否显示')->display(function($value) {
            return Classify::$isShowLabels[$value];
        })->sortable();
        //$grid->column('book_ct', __('书本数'))->sortable();
        $grid->column('created_at', __('创建时间'));
        //$grid->column('updated_at', __('更新时间'));

        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->like('name','名称');

            $filter->equal('is_show','是否显示')->radio(Classify::$isShowLabels);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Classify::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('sort', __('Sort'));
        $show->field('is_show', __('Is show'));
        $show->field('book_ct', __('Book ct'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Classify);

        $form->image('icon', __('icon'))->move('book/classify/icon')->uniqueName();
        $form->text('name', __('名称'))->rules('required');
        $form->text('description', __('描述'))->rules('required');
        $form->number('sort', __('排序'))->default(0)->rules('required');
        $form->radio('is_show', '是否显示')->options(Classify::$isShowLabels)->default(1);
        //$form->number('book_ct', __('Book ct'));

        return $form;
    }
}
